import random
import string

from sqlalchemy import BigInteger, Column, String, Boolean, DateTime
from settings import DATACENTER_ID, WORKER_ID
from utils.snowflake.snowflake import Snowflake
from . import Base
from sqlalchemy_serializer import SerializerMixin
snowflake = Snowflake(DATACENTER_ID, WORKER_ID)
def generate_username():
    code = "".join(random.sample(string.digits, 6))
    return "淘的多" + code

def generate_snowflake_id():
    new_id = snowflake.get_id()
    return new_id

class User(Base,SerializerMixin):
    __tablename__= 'user'
    # serialize_rules = ('-password', '-addresses')
    serialize_only = ('id', 'mobile', 'username', 'is_active', 'avatar', 'is_staff')
    id = Column(BigInteger, primary_key=True, default=generate_snowflake_id)
    mobile = Column(String(20),unique=True,index=False)
    username = Column(String(20),  nullable=True,default=generate_username)
    password = Column(String(200), nullable=True)
    avatar = Column(String(200), nullable=True)
    is_active = Column(Boolean, default=True)
    is_staff = Column(Boolean, default=False)
    last_login = Column(DateTime, nullable=True)
